<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-10-10
 * Time: 上午10:03
 */
class ProductModel extends BaseModel{
    private $_table;
    protected $_name = 'product';
    protected $_isbug = false;

    public function __construct() {
        parent::__construct();
    }


    public function getProductIdByWhere($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();

        $sql = $db->select ()->from ( $this->_name,'id')
            ->joinLeft ( "product_cates", "$this->_name.cate_id = product_cates.id",'')
            ->joinLeft ( "product_brands", "$this->_name.brand_id = product_brands.id",'')
            ->where ( $where )->__toString ();
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchOne( $sql );

    }
##########################################################

    public function getProdutByName($product_name,$brand_name,$cate_name){
        $this->_table = new self();
        $db = $this->_table->getAdapter();
        $select = $db->select();
        $select->from("product as p",array("id"))
            ->joinLeft("product_brands as pb","p.brand_id = pb.id","")
            ->joinLeft("product_cates as pc","pc.id = p.cate_id","")
            ->where("p.product_name = '{$product_name}' and pb.brand_name = '{$brand_name}' and pc.cate_name = '{$cate_name}'");
        //todo 方便调试开启输出SQL语句
        $sql = $select->__toString();
        if($this->_isbug == true){
            echo $sql;
        }
        $result = $db->fetchOne($sql);
        return $result;
    }

    public function addProdutByName($product_name,$brand_name,$cate_name){
        $this->_table = new self();
        $db = $this->_table->getAdapter();
        $select1 = $db->select();
        $select2 = $db->select();
        $db->beginTransaction();
        try {
            if(!empty($brand_name)){
                $bsql = $select1->from("product_brands",array('id'))->where("brand_name = '{$brand_name}'")->__toString();
                $brand_id = $db->fetchOne($bsql);
                if(!$brand_id){
                    $res =  $db->insert("product_brands",array('brand_name'=>$brand_name));
                    $brand_id = $db->lastInsertId("product_brands");
                }
            }
            if(!empty($cate_name)){
                $csql =  $select2->from("product_cates",array('id'))->where("cate_name = '{$cate_name}'")->__toString();
                $cate_id = $db->fetchOne($csql);
                if(!$cate_id){
                    $res =  $db->insert("product_cates",array('cate_name'=>$cate_name));
                    $cate_id = $db->lastInsertId("product_cates");
                }
            }

            $data = array(
                'product_code' =>  $product_name.$brand_name,
                'original_name' => $cate_name.$product_name.$brand_name,
                'product_name' => $product_name,
                'brand_id' => $brand_id,
                'cate_id' => $cate_id,
                'uom_id' =>1,
                'status' =>1,
                'remark'=>'发布报价的时候，自动添加的产品'
            );
            $res =  $db->insert("product",$data);
            $product_id = $db->lastInsertId("product");
            $db->commit();
            return $product_id;
        }catch (Exception $e){
            $db->rollBack();
            echo $e->getMessage();
        }
    }

    /**
     * @param $key
     * @return string
     */
    public function getProductListCountByWhere($key){
        $this->_table = new self();
        $db = $this->_table->getAdapter();
        $select = $db->select();
        $select->from("product as p",array("count(*)"))
            ->joinLeft("product_brands as pb","p.brand_id = pb.id","")
            ->joinLeft("product_cates as pc","pc.id = p.cate_id","")
            ->where("p.product_name like '{$key}' or pb.brand_name like '{$key}' or pc.cate_name like '{$key}'");
        //todo 方便调试开启输出SQL语句
        $sql = $select->__toString();
        if($this->_isbug == true){
            echo $sql;
        }
        $result = $db->fetchOne($sql);
        return $result;
    }

    public function getProductListPageByWhere($key,$sidx,$sord,$start=0,$limit=0){
        $this->_table = new self();
        $db = $this->_table->getAdapter();
        $select = $db->select();
        $select->from("product as p",array("product_id","product_name"))
            ->joinLeft("product_brands as pb","p.brand_id = pb.id","brand_name")
            ->joinLeft("product_cates as pc","pc.id = p.cate_id","cate_name")
            ->where("p.product_name like '{$key}' or pb.brand_name like '{$key}' or pc.cate_name like '{$key}'");
        //todo 方便调试开启输出SQL语句
        $sql = $select->__toString();
//        $sql.=" ORDER BY $sidx $sord";
//        if($start&&$limit){
//            $sql .=" limit $start,$limit";
//        }

        if($this->_isbug == true){
            echo $sql;
        }
        $result = $db->fetchAll($sql);
        return $result;
    }
}